
Imports System
Imports System.Collections
Imports System.ComponentModel
Imports System.Data
Imports System.Drawing
Imports System.Web
Imports System.Web.SessionState
Imports System.Web.UI
Imports System.Web.UI.WebControls
Imports System.Web.UI.HtmlControls
Imports _2bMatesSite.BO

Namespace _2bMatesSite
	''' <summary>
	''' Summary description for group_profile.
	''' </summary>
	Public Partial Class create_group
		Inherits BaseWebPage

		Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs)
			' Put user code to initialize the page here
			Me.pageTemplate.PageTitle = PageTitles.CREATE_GROUP_PAGE

			Dim usr As User = LoggedUser
			If usr = Nothing Then
				Common.Utils.RedirectToLoginPage(Page)
			End If

			' init page from XML
			Me.initPageTitles(Constants.CREATE_GROUP, "", Form1, Nothing, Nothing)
			Me.initAdvertisements(Form1, Nothing)

			' init tabs 
			ucGroupTabs.ActiveTab = 4
			ucGroupTabs.PageName = Constants.CREATE_GROUP

			If Constants.SHOW_GROUPS <> Constants.SHOW_ON Then
				Throw New Exception(Constants.ERROR_MESSAGE_NO_ACCESS)
			End If

			Dim i As Integer

			If Not Me.IsPostBack Then
				' init categories dropdown
				Dim tmpLI As ListItem
				Dim categoriesCount As Integer = Constants.CATEGORIES.List.Count
				Dim tmpCategory As Categories.Category

				i = 0
				While i < categoriesCount
					tmpCategory = DirectCast(Constants.CATEGORIES.List(i), Categories.Category)
					tmpLI = New ListItem(tmpCategory.Name, tmpCategory.ID.ToString())
					ddlCategories.Items.Add(tmpLI)
					System.Math.Max(System.Threading.Interlocked.Increment(i),i - 1)
				End While

				' select the default category
				ddlCategories.ClearSelection()
				ddlCategories.Items.FindByText(Constants.DEFAULT_CATEGORY_NAME).Selected = True

				' init countries dropdown

				Dim countriesCount As Integer = Constants.COUNTRIES.List.Count
				Dim tempCountry As Countries.Country

				i = 0
				While i < countriesCount
					tempCountry = DirectCast(Constants.COUNTRIES.List(i), Countries.Country)
					ddlCountry.Items.Add(tempCountry.Name)
					ddlCountry.Items.FindByText(tempCountry.Name).Value = tempCountry.ID.ToString()
					System.Math.Max(System.Threading.Interlocked.Increment(i),i - 1)
				End While

				' select UK by default
				ddlCountry.ClearSelection()
				ddlCountry.Items.FindByValue(Countries.UKID.ToString()).Selected = True
			End If

			txtAboutGroup.Attributes.Add("onKeyDown", Constants.ON_KEY_DOWN_SCRIPT.Replace(Constants.TEXT_AREA_MAX_CHARS_TAG, Constants.TEXT_AREA_GROUP_MAX_CHARS.ToString()))
			txtAboutGroup.Attributes.Add("onKeyUp", Constants.ON_KEY_UP_SCRIPT.Replace(Constants.TEXT_AREA_MAX_CHARS_TAG, Constants.TEXT_AREA_GROUP_MAX_CHARS.ToString()))
			txtShortAboutGroup.Attributes.Add("onKeyDown", Constants.ON_KEY_DOWN_SCRIPT.Replace(Constants.TEXT_AREA_MAX_CHARS_TAG, Constants.TEXT_AREA_SHORT_GROUP_MAX_CHARS.ToString()))
			txtShortAboutGroup.Attributes.Add("onKeyUp", Constants.ON_KEY_UP_SCRIPT.Replace(Constants.TEXT_AREA_MAX_CHARS_TAG, Constants.TEXT_AREA_SHORT_GROUP_MAX_CHARS.ToString()))

			' init help links
			hlHelpPublic.NavigateUrl = "../#public"
			hlHelpPublic.Attributes.Add("onclick", "popWinHelp('" + Constants.HELP_GROUPS + "?" + Constants.GROUP_HELP_PAGE_PARAM + "=" + Constants.GROUP_HELP_PAGE_PUBLIC_VALUE + "')")
			hlHelpAppMemComm.NavigateUrl = "../#appcomm"
			hlHelpAppMemComm.Attributes.Add("onclick", "popWinHelp('" + Constants.HELP_GROUPS + "?" + Constants.GROUP_HELP_PAGE_PARAM + "=" + Constants.GROUP_HELP_PAGE_APP_MEM_COMM_VALUE + "')")
			hlHelpAppMemBlogComm.NavigateUrl = "../#appblogcomm"
			hlHelpAppMemBlogComm.Attributes.Add("onclick", "popWinHelp('" + Constants.HELP_GROUPS + "?" + Constants.GROUP_HELP_PAGE_PARAM + "=" + Constants.GROUP_HELP_PAGE_APP_MEM_BLOG_COMM_VALUE + "')")
		End Sub

		#region Web Form Designer generated code
		Protected Overloads Overrides Sub OnInit(ByVal e As EventArgs)
			'
			' CODEGEN: This call is required by the ASP.NET Web Form Designer.
			'
			InitializeComponent()
			MyBase.OnInit(e)
		End Sub

		''' <summary>
		''' Required method for Designer support - do not modify
		''' the contents of this method with the code editor.
		''' </summary>
		Private Sub InitializeComponent()
			AddHandler Me.cvGroupExist.ServerValidate, AddressOf Me.cvGroupExist_ServerValidate
			AddHandler Me.cvHtmlInGroupName.ServerValidate, AddressOf Me.cvHtmlInGroupName_ServerValidate
			AddHandler Me.cvHtmlInCity.ServerValidate, AddressOf Me.cvHtmlInCity_ServerValidate
			AddHandler Me.cvHtmlInZip.ServerValidate, AddressOf Me.cvHtmlInZip_ServerValidate
			AddHandler Me.cvHtmlInShortDesc.ServerValidate, AddressOf Me.cvHtmlInShortDesc_ServerValidate
			AddHandler Me.cvZip.ServerValidate, AddressOf Me.cvZip_ServerValidate

		End Sub
#End Region

		Private Sub InitGroup(ByVal group As Group)
			Dim temp As String = ""

			temp = removeScriptTags(Request.Params(txtGroupName.UniqueID).Trim())
			group.Name = temp.Substring(0, Math.Min(temp.Length, Constants.TEXT_FIELD_DEFAULT_MAX_CHARS))

			group.CatID = Convert.ToInt32(Request.Params(ddlCategories.UniqueID).Trim())

			group.PublicGroup = Convert.ToInt32(Request.Params(rbPublic.NamingContainer.UniqueID + ":" + rbPublic.GroupName)) = 1

			group.AutoApproveComments = Convert.ToInt32(Request.Params(rbApproveMemComm.NamingContainer.UniqueID + ":" + rbApproveMemComm.GroupName)) = 0

			group.AutoApproveBlogComments = Convert.ToInt32(Request.Params(rbApproveMemBlogComm.NamingContainer.UniqueID + ":" + rbApproveMemBlogComm.GroupName)) = 0

			group.CountryID = Convert.ToInt32(Request.Params(ddlCountry.UniqueID).Trim())

			temp = removeScriptTags(Request.Params(txtCity.UniqueID).Trim())
			group.City = temp.Substring(0, Math.Min(temp.Length, Constants.TEXT_FIELD_DEFAULT_MAX_CHARS))

			temp = removeScriptTags(Request.Params(txtZip.UniqueID).Trim())
			group.ZipCode = temp.Substring(0, Math.Min(temp.Length, Constants.TEXT_FIELD_ZIPCODE_CHARS))

			temp = removeScriptTags(Request.Params(txtAboutGroup.UniqueID).Trim())
			group.About = temp.Substring(0, Math.Min(temp.Length, Constants.TEXT_AREA_GROUP_MAX_CHARS))

			temp = removeScriptTags(Request.Params(txtShortAboutGroup.UniqueID).Trim())
			group.ShortAbout = temp.Substring(0, Math.Min(temp.Length, Constants.TEXT_AREA_SHORT_GROUP_MAX_CHARS))
		End Sub

		Protected Sub btnCreate_Click(ByVal sender As Object, ByVal e As System.EventArgs)
			If Page.IsValid Then
				Dim grp As New Group(LoggedUser.ID)
				InitGroup(grp)
				Dim grpTemp As Group = Groups.GetGroupByCatIDName(grp.CatID, grp.Name)
				If grpTemp = Nothing Then
					grp.Save()
					Constants.CATEGORIES = New BO.Categories()
					Response.Redirect(Constants.UPLOAD_GROUP_PHOTO + "?" + Constants.REGISTRATION_PARAM + "=" + Constants.REGISTRATION_VALUE + "&" + Constants.GROUP_PARAM + "=" + grp.ID)
				End If
			End If
		End Sub

		Private Sub cvZip_ServerValidate(ByVal source As Object, ByVal args As System.Web.UI.WebControls.ServerValidateEventArgs)
			Dim countryID As Integer = Convert.ToInt32(Request.Params(ddlCountry.UniqueID))
			If (countryID = Countries.USAID OrElse countryID = Countries.CANADAID) AndAlso Request.Params(txtZip.UniqueID) = "" Then
				args.IsValid = False
			Else
				args.IsValid = True
			End If
		End Sub

		Private Sub cvGroupExist_ServerValidate(ByVal source As Object, ByVal args As System.Web.UI.WebControls.ServerValidateEventArgs)
			Dim temp As String
			Dim nameValue As String = ""
			Dim catID As Integer = Convert.ToInt32(Request.Params(ddlCategories.UniqueID))

			temp = removeScriptTags(txtGroupName.Text.Trim())
			nameValue = temp.Substring(0, Math.Min(temp.Length, Constants.TEXT_FIELD_DEFAULT_MAX_CHARS))

			Dim grp As Group = Groups.GetGroupByCatIDName(catID, nameValue)
			If grp <> Nothing Then
				args.IsValid = False
			Else
				args.IsValid = True
			End If
		End Sub

		Private Sub cvHtmlInGroupName_ServerValidate(ByVal source As Object, ByVal args As System.Web.UI.WebControls.ServerValidateEventArgs)
			Dim gn As String = Request.Form(txtGroupName.UniqueID)

			If isHTML(gn) Then
				args.IsValid = False
			Else
				args.IsValid = True
			End If
		End Sub

		Private Sub cvHtmlInCity_ServerValidate(ByVal source As Object, ByVal args As System.Web.UI.WebControls.ServerValidateEventArgs)
			Dim city As String = Request.Form(txtCity.UniqueID)

			If isHTML(city) Then
				args.IsValid = False
			Else
				args.IsValid = True
			End If
		End Sub

		Private Sub cvHtmlInZip_ServerValidate(ByVal source As Object, ByVal args As System.Web.UI.WebControls.ServerValidateEventArgs)
			Dim zip As String = Request.Form(txtZip.UniqueID)

			If isHTML(zip) Then
				args.IsValid = False
			Else
				args.IsValid = True
			End If
		End Sub

		Private Sub cvHtmlInShortDesc_ServerValidate(ByVal source As Object, ByVal args As System.Web.UI.WebControls.ServerValidateEventArgs)
			Dim sd As String = Request.Form(txtShortAboutGroup.UniqueID)

			If isHTML(sd) Then
				args.IsValid = False
			Else
				args.IsValid = True
			End If
		End Sub

		====================
        ' This public property was added by conversion wizard to allow the access of a protected, autogenerated member variable Form1.
		====================
        Public ReadOnly Property Form1() As System.Web.UI.HtmlControls.HtmlForm
            Get
                Return Migrated_Form1
            End Get
        End Property
        'set { Migrated_Form1 = value; }
    End Class
End Namespace






